import baseapi from '@/api/base'
import { getDataByParameter, getObjetInfoByPid, getCountByParameter, saveObjetInfo, deleteObjetInfo ,postInfo} from '@/utils/baserequest'
import storage from 'store'

const Base64 = require('js-base64').Base64

const api = {
  appContext: baseapi.biz,
  appAppId: baseapi.biz_app,
  appServer: 'deliveryOrder',
  appServer2: 'deliveryOrderDetail/',

  getByConditions: 'deliveryOrder/by/findLikeExample',
  getByConditionsCount: 'deliveryOrder/countLikeExample/',
  //配送单发货   //配送单作废接口
  sendOrder:'deliveryOrder/state',
  //配送单整单收货(配送单收货) //配送单作废接口
  receiveOrder:'deliveryOrder/received', 
  //配送单批量收货(商品收货)
  receiveGoods:'deliveryOrderDetail/batchEdit',
  //保存订单商品信息deliveryOrderDetail/batchEdit/
  saveOederGoods:'deliveryOrderDetail/batchEdit',
  editOederGoods:'deliveryOrderDetail/batchEdit',
  getDetailByConditions: 'deliveryOrderDetail/by/findLikeExample',
  getDetailByConditionsCount: 'deliveryOrderDetail/countLikeExample/',
  //合并配送
  getMergeByConditions: 'saleOrderDetail/groupSum',
  //提交合并配送
  saveMerge:'scm/createDeliveryOrder/',
  //查询合并配送单
  getMergeOrder:'scm/by/deliveryInfo/',
  //合并配送单商品明细的新增和修改 删除
  saveMergeDetail:'deliveryOrderDetail/',
  //删除合并配送单商品 //
  saveMergeGoods:'deliveryDetail/',
  //校验配送单明细数量
  checkDetailNum:'scm/checkNumDeliveryOrder/'
}
export default api
//合并配送
export function getMergeByConditions(parameter, pagination, headers) {
  const appIdHeaders = {
    appId: api.appAppId,
    org: storage.get('org'),
    authOption: (headers && headers.authOption && headers.authOption !== 'undefined') ? headers.authOption : '1'
  }
  // const pa = {
  //   example: Base64.encode(JSON.stringify(Object.assign( parameter,  {orgPid: storage.get('org') }))),
  //   pageSize: pagination.all?10000:pagination.pageSize,
  //   currentPage: pagination.current,
  //   sidx: "",
  //   order: "0"
  // }
  const url = api.appContext + api.getMergeByConditions
  const allheaders = appIdHeaders
  return getDataByParameter(url, parameter, allheaders)
}
//配送单明细数量校验
export function checkDetailNum(parameter) {
  const appIdHeaders = {
    appId: api.appAppId,
    org: storage.get('org')
  }
  const url = api.appContext + api.checkDetailNum
  const allheaders = appIdHeaders
  return postInfo(url, parameter, allheaders)
}
//提交合并配送
export function saveMerge(parameter) {
  const appIdHeaders = {
    appId: api.appAppId,
    org: storage.get('org')
  }
  const url = api.appContext + api.saveMerge
  const allheaders = appIdHeaders
  return postInfo(url, parameter, allheaders)
}
//查询合并配送单
export function getMergeOrder(parameter, pagination, headers) {
  const appIdHeaders = {
    appId: api.appAppId,
    org: storage.get('org'),
    authOption: (headers && headers.authOption && headers.authOption !== 'undefined') ? headers.authOption : '1'
  }
  const pa = {
    example: Base64.encode(JSON.stringify(Object.assign( parameter,  {orgPid: storage.get('org') }))),
    pageSize: pagination.all?10000:pagination.pageSize,
    currentPage: pagination.current,
    sidx: "",
    order: "0"
  }
  const url = api.appContext + api.getMergeOrder
  const allheaders = appIdHeaders
  return getDataByParameter(url, pa, allheaders)
}
//合并配送单商品明细的新增和修改
export function saveMergeDetail(parameter) {
  const appIdHeaders = {
    appId: api.appAppId,
    org: storage.get('org')
  }
  const url = api.appContext + api.saveMergeDetail 
  const allheaders = appIdHeaders
  return saveObjetInfo(url, parameter, allheaders)
}
//删除合并配送单商品明细
export function deleteMergeDetail(parameter) {
  const appIdHeaders = {
    appId: api.appAppId,
    org: storage.get('org')
  }
  const url = api.appContext + api.saveMergeDetail 
  const allheaders = appIdHeaders
  return deleteObjetInfo(url, parameter, allheaders)
}
//删除合并配送单商品
export function deleteMergeGoods(parameter) {
  const appIdHeaders = {
    appId: api.appAppId,
    org: storage.get('org')
  }
  const url = api.appContext + api.saveMergeGoods 
  const allheaders = appIdHeaders
  return deleteObjetInfo(url, parameter, allheaders)
}
//多条件查询
export function getByConditions(parameter, pagination, headers) {
  const appIdHeaders = {
    appId: api.appAppId,
    org: storage.get('org'),
    authOption: (headers && headers.authOption && headers.authOption !== 'undefined') ? headers.authOption : '1'
  }
  const pa = {
    example: Base64.encode(JSON.stringify(Object.assign( parameter,  {orgPid: storage.get('org') }))),
    pageSize: pagination.all?10000:pagination.pageSize,
    currentPage: pagination.current,
    sidx: "",
    order: "0"
  }
  const url = api.appContext + api.getByConditions
  const allheaders = appIdHeaders
  return getDataByParameter(url, pa, allheaders)
}
//多条件查询列表条数
export function getByConditionsCount(parameter, headers) {
  const appIdHeaders = {
    appId: api.appAppId,
    org: storage.get('org'),
    authOption: (headers && headers.authOption && headers.authOption !== 'undefined') ? headers.authOption : '1'
  }
  const example = Object.assign( parameter,  {orgPid: storage.get('org') })
  const url = api.appContext + api.getByConditionsCount
  const allheaders = appIdHeaders
  return getCountByParameter(url, Base64.encode(JSON.stringify(example)), allheaders)
}
//新增和修改
export function saveInfo(parameter) {
  const appIdHeaders = {
    appId: api.appAppId,
    org: storage.get('org')
  }
  const url = api.appContext + api.appServer + '/'
  const allheaders = appIdHeaders
  return saveObjetInfo(url, parameter, allheaders)
}
//删除记录
export function deleteInfo(parameter) {
  const appIdHeaders = {
    appId: api.appAppId,
    org: storage.get('org')
  }
  const url = api.appContext + api.appServer + '/'
  const allheaders = appIdHeaders
  return deleteObjetInfo(url, parameter, allheaders)
}
//发货
export function sendOrder(parameter) {
  const appIdHeaders = {
    appId: api.appAppId,
    org: storage.get('org')
  }
  const url = api.appContext + api.sendOrder
  const allheaders = appIdHeaders
  return postInfo(url, parameter, allheaders)
}
//配送单整单收货(配送单收货)
export function receiveOrder(parameter) {
  const appIdHeaders = {
    appId: api.appAppId,
    org: storage.get('org')
  }
  const url = api.appContext + api.receiveOrder
  const allheaders = appIdHeaders
  return postInfo(url, parameter, allheaders)
}
//配送单批量收货(商品收货)
export function receiveGoods(parameter) {
  const appIdHeaders = {
    appId: api.appAppId,
    org: storage.get('org')
  }
  const url = api.appContext + api.receiveGoods
  const allheaders = appIdHeaders
  return postInfo(url, parameter, allheaders)
}
//删除明细
export function deleteDetailInfo(parameter) {
  const appIdHeaders = {
    appId: api.appAppId,
    org: storage.get('org')
  }
  const url = api.appContext + api.appServer2 + '/'
  const allheaders = appIdHeaders
  return deleteObjetInfo(url, parameter, allheaders)
}
// 通过pid得到信息通用请求:get请求
export function getInfoByPid(parameter, headers) {
  const appIdHeaders = {
    appId: api.appAppId,
    org: storage.get('org')
  }
  const url = api.appContext + api.appServer
  const allheaders = appIdHeaders
  return getObjetInfoByPid(url, parameter, allheaders)
}

//删除订单单个商品信息
export function deleteGoodsInfo(parameter) {
  const appIdHeaders = {
    appId: api.appAppId,
    org: storage.get('org')
  }
  const url = api.appContext + api.appServer2 + '/'
  const allheaders = appIdHeaders
  return deleteObjetInfo(url, parameter, allheaders)
}
//保存订单商品信息 
export function saveOederGoods(parameter) {
  const appIdHeaders = {
    appId: api.appAppId,
    org: storage.get('org')
  }
  const url = api.appContext + api.saveOederGoods
  const allheaders = appIdHeaders
  return postInfo(url, parameter, allheaders)
}

//保存订单商品信息 
export function editOederGoods(parameter) {
  const appIdHeaders = {
    appId: api.appAppId,
    org: storage.get('org')
  }
  const url = api.appContext + api.editOederGoods
  const allheaders = appIdHeaders
  return postInfo(url, parameter, allheaders)
}

//多条件查询明细商品
export function getDetailByConditions(parameter, pagination, headers) {
  const appIdHeaders = {
    appId: api.appAppId,
    org: storage.get('org'),
    authOption: (headers && headers.authOption && headers.authOption !== 'undefined') ? headers.authOption : '1'
  }
  const pa = {
    example: Base64.encode(JSON.stringify(Object.assign( parameter,  {orgPid: storage.get('org') }))),
    pageSize: pagination.pageSize,
    currentPage: pagination.current,
    sidx: "",
    order: "0"
  }
  const url = api.appContext + api.getDetailByConditions
  const allheaders = appIdHeaders
  return getDataByParameter(url, pa, allheaders)
}
//多条件查询列表条数
export function getDetailByConditionsCount(parameter, headers) {
  const appIdHeaders = {
    appId: api.appAppId,
    org: storage.get('org'),
    authOption: (headers && headers.authOption && headers.authOption !== 'undefined') ? headers.authOption : '1'
  }
  const example = Object.assign( parameter,  {orgPid: storage.get('org') })
  const url = api.appContext + api.getDetailByConditionsCount
  const allheaders = appIdHeaders
  return getCountByParameter(url, Base64.encode(JSON.stringify(example)), allheaders)
}








